<template>
  <el-row>
    <el-card>
      <el-form ref="searchForm" :inline="true" :model="searchForm">
        <el-form-item :label="tableText['checkInTime']">
          <el-date-picker
            v-model="time"
            type="daterange"
            range-separator="~"
            :start-placeholder="$t('common.startDate')"
            :end-placeholder="$t('common.endDate')"
          />
        </el-form-item>
        <el-form-item :label="tableText['reviewStage']">
          <el-select v-model="searchForm.stageId" :placeholder="$t('administrator.attendanceRecord.prompt.reviewStage')">
            <el-option
              v-for="item in statusData"
              :key="item.id"
              :label="item.flowName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="tableText['company']">
          <el-select v-model="searchForm.firmId" clearable :placeholder="$t('administrator.attendanceRecord.prompt.company')">
            <el-option
              v-for="item in allFirm"
              :key="`${item.id}_firm`"
              :label="item['firmName']"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="tableText['group']">
          <el-select v-model="searchForm.groupId" :placeholder="$t('administrator.attendanceRecord.prompt.group')">
            <el-option-group
              v-for="group in allGroup"
              :key="group.label"
              :label="group.label"
            >
              <el-option
                v-for="item in group.options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-option-group>
          </el-select>
        </el-form-item>
        <el-form-item :label="tableText['account']">
          <el-input v-model="searchForm.riskEmployeeAccount" clearable :placeholder="$t('administrator.attendanceRecord.prompt.account')" :maxlength="50" />
        </el-form-item>
        <el-form-item :label="tableText['name']">
          <el-input v-model="searchForm.riskEmployeeName" clearable :placeholder="$t('administrator.attendanceRecord.prompt.name')" :maxlength="50" />
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
          <el-button :disabled="!(totalNumber > 0)" @click="exportList">
            {{ $t('common.ex') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        :stripe="true"
        style="width: 100%"
      >
        <el-table-column
          fixed="left"
          prop="staffId"
          :label="tableText['id']"
          min-width="100"
        />
        <el-table-column
          fixed="left"
          prop="riskEmployeeAccount"
          :label="tableText['account']"
          min-width="120"
        />
        <el-table-column
          fixed="left"
          prop="riskEmployeeName"
          :label="tableText['name']"
          min-width="120"
        />
        <el-table-column
          prop="groupName"
          :label="tableText['group']"
          min-width="120"
        />
        <el-table-column
          prop="stageName"
          :label="tableText['manualReviewStage']"
          min-width="120"
        />
        <el-table-column
          prop="firmName"
          :label="tableText['company']"
          min-width="120"
        />
        <el-table-column
          prop="firmType"
          :label="tableText['companyType']"
          min-width="200"
        >
          <template slot-scope="scope">
            {{ setFirmType(scope.row['firmType']) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="signInDate"
          :label="tableText['checkInTime']"
          min-width="170"
        />
        <el-table-column
          prop="signOutDate"
          :label="tableText['checkOutTime']"
          min-width="170"
        />
        <el-table-column
          prop="totalAllocateCount"
          :label="tableText['allocatedOrders']"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ scope.row['totalAllocateCount'] || 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="auditCount"
          :label="tableText['reviews']"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ scope.row['auditCount'] || 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="auditPassCount"
          :label="tableText['audits']"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ scope.row['auditPassCount'] || 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="auditRefuseCount"
          :label="tableText['reviewRejection']"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ scope.row['auditRefuseCount'] || 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="unAuditCount"
          :label="tableText['unReviewed']"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ scope.row['unAuditCount'] || 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="workTime"
          :label="tableText['workingHours']"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ scope.row['workTime'] || 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="hourAuditCount"
          :label="tableText['trialsTime']"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ scope.row['hourAuditCount'] || 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="auditPassRate"
          :label="tableText['auditPassRate']"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ scope.row['auditPassRate'] ? (scope.row['auditPassRate'] * 100).toFixed(2) + '%' : 0 }}
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="searchForm.currentPage"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="searchForm.pageSize"
          :total="totalNumber"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </el-row>
</template>

<script>
import { listAuditStage, attendance, exportAttendance, getAllCreditReviewFirm, getAllCreditReviewGroup } from '@/api/risk'
import { formatDate } from '@/utils/method'
export default {
  name: 'AttendanceRecord',
  data() {
    return {
      loading: true,
      tableText: {},
      totalNumber: 0,
      time: null,
      statusData: [],
      allFirm: [],
      allGroup: [],
      tableData: [],
      promotionData: [],
      allPackageData: [],
      searchForm: this.params()
    }
  },
  mounted() {
    this.tableText = this.$t('administrator.attendanceRecord')
    this.getStageData()
    this.getAllFirm()
    this.getData()
  },
  methods: {
    params() {
      return {
        currentPage: 1,
        pageSize: 10,
        signInBeginDate: null,
        signInEndDate: null,
        stageId: '',
        firmId: '',
        groupId: null,
        riskEmployeeAccount: null,
        riskEmployeeName: null
      }
    },
    search() {
      this.searchForm.currentPage = 1
      this.getData()
    },
    reset() {
      this.searchForm = this.params()
      this.time = null
      this.getData()
    },
    searchData() {
      const data = this.searchForm
      if (this.time && this.time.length > 1) {
        data['signInBeginDate'] = formatDate(new Date(this.time[0]), 'yyyy-MM-dd') + ' 00:00:00'
        data['signInEndDate'] = formatDate(new Date(this.time[1]), 'yyyy-MM-dd') + ' 23:59:59'
      } else {
        data['signInBeginDate'] = null
        data['signInEndDate'] = null
      }
      return data
    },
    getData() {
      this.loading = true
      attendance(this.searchData())
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = res.data
            this.totalNumber = res.page ? res.page['totalNumber'] : 0
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    setFirmType(firmType) {
      if (firmType !== null) {
        const name = this.tableText['companyTypes'].filter(item => {
          return item.value === firmType
        })
        return (name.length > 0 && name[0].desc) ? name[0].desc : ''
      }
    },
    exportList() {
      exportAttendance(this.searchData())
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val
      this.getData()
    },
    getStageData() {
      listAuditStage()
        .then(res => {
          if (res.success) {
            if (res.data && res.data.length > 0) {
              this.statusData = res.data
              const obj = [{
                flowName: this.$t('common.all'),
                id: ''
              }]
              this.statusData = obj.concat(this.statusData)
            } else {
              this.statusData = this.tableText['status']
            }
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    },
    getAllFirm() {
      getAllCreditReviewFirm()
        .then(res => {
          if (res.success) {
            this.allFirm = res.data.filter(item => {
              return item['status'] === 1
            })
            const obj = [{
              firmName: this.$t('common.all'),
              id: ''
            }]
            this.allFirm = obj.concat(this.allFirm)
            this.getAllGroup()
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    },
    getAllGroup() {
      getAllCreditReviewGroup()
        .then(res => {
          if (res.success) {
            const arr = []
            res.data.map(item => {
              if (!arr.includes(item['firmId'])) {
                arr.push(item['firmId'])
              }
            })
            const group = []
            if (arr.length > 0) {
              arr.forEach(v => {
                const options = []
                const a = res.data.filter(item => {
                  return item['firmId'] === v
                })
                a.forEach(item => {
                  options.push(
                    {
                      label: item['groupName'],
                      value: item['id']
                    }
                  )
                })
                const name = this.allFirm.filter(aa => {
                  return aa['id'] === v
                })
                const firmName = name && name[0] && name[0]['firmName'] ? name[0]['firmName'] : 0
                group.push({
                  label: firmName,
                  value: firmName,
                  options
                })
              })
            }
            this.allGroup = group
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card{
  margin: 10px;
}
</style>
